package com.mdd.admin.controller.system;

import java.util.*;

import com.mdd.admin.config.AdminConfig;
import com.mdd.common.entity.system.SystemAuthMenu;
import com.mdd.common.mapper.system.SystemAuthMenuMapper;
import com.mdd.common.util.RedisUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import javax.annotation.Resource;
import java.util.List;

/**
 * 系统菜单管理
 */
@RestController
@RequestMapping("api/system/getAPI")
public class SystemApiController {

        @Autowired
        WebApplicationContext applicationContext;

    @Resource
    SystemAuthMenuMapper systemAuthMenuMapper;

        @ResponseBody
        @RequestMapping("/getAllUrl")
        public List<Map<String, String>> getAllUrl(@RequestParam("update") String update) {
            // 获取springmvc处理器映射器组件对象 RequestMappingHandlerMapping无法直接注入
            RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
            //获取url与类和方法的对应信息
            Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
                Map<String, String> mapone = new HashMap<String, String>();
                RequestMappingInfo info = m.getKey();
                HandlerMethod method = m.getValue();
                // 获取url
                PatternsRequestCondition p = info.getPatternsCondition();
                for (String url : p.getPatterns()) {
                    url= url.replaceAll("/api/","");
                    url  = url.replace("/", ":");
                    mapone.put("url", url);
                }
                // 反射获取url对应类名和方法名
                mapone.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
                mapone.put("method", "API:"+method.getMethod().getName()); // 方法名

                // 获取请求类型
                RequestMethodsRequestCondition methodsRequestCondition = info.getMethodsCondition();
                for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) {
                    mapone.put("type", requestMethod.toString());
                }
                list.add(mapone);
            }

            if("true".equals(update)){
                // 先删除  后新增
                systemAuthMenuMapper.deleteMenuApiPermissionInfo();
                systemAuthMenuMapper.deleteMenuApiInfo();
                Integer permissionMenuId = systemAuthMenuMapper.getPermissionMenuId();
                for (Map<String,String> resMap:list) {
                    SystemAuthMenu model = new SystemAuthMenu();
                    model.setPid(permissionMenuId);
                    model.setMenuType("A");
                    model.setMenuName(resMap.get("method"));
                    model.setMenuSort(0);
                    model.setPerms(resMap.get("url"));
                    model.setIsCache(0);
                    model.setIsShow(1);
                    model.setIsDisable(0);
                    model.setCreateTime(System.currentTimeMillis() / 1000);
                    model.setUpdateTime(System.currentTimeMillis() / 1000);
                    systemAuthMenuMapper.insert(model);
                }
            }
            return list;
        }
    }
